#
#   user.py
#   journalismManger
#
#   Created by luoxh on 2019-07-20-17:07.
#   Copyright © 2019 luoxh. All rights reserved.
#
from flask import request
from flask_jwt import (current_identity, jwt_required, )

from src.extension.redprint import Redprint
from src.model.user import User
from src.validator.v1.user import valid_user_register
from src.viewmodel.user import UserVM

user_rp = Redprint('user')


@user_rp.route('/register', methods=['POST'])
def register():
    username, password = valid_user_register(request)
    user = User(username=username)
    user.password = password
    user.save()
    return {'code': 200, 'data': None}


@user_rp.route('/profile', methods=['GET', 'PUT'])
@jwt_required()
def profile():
    return {'code': 200, 'data': UserVM(current_identity)}
